package com.hh.releasenode.config;


import com.hh.releasenode.shiro.OAuth2Filter;
import com.hh.releasenode.shiro.ShiroRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.shiro.mgt.SecurityManager;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * @ClassName ShiroConfig
 * @Description shiro 安全框架配置类
 * @Author KunxianLi
 * Date 2020/6/8 15:12
 * Version 1.0
 **/
//@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        HashMap<String, Filter> filters = new HashMap<>();

        filters.put("oauth2",new OAuth2Filter());
        shiroFilterFactoryBean.setFilters(filters);
        //登录页面路径
        shiroFilterFactoryBean.setLoginUrl("/admin/login");
        //登陆成功后跳转页面
//        shiroFilterFactoryBean.setSuccessUrl("/hell/test");
        //权限不足跳转页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/403");

        LinkedHashMap<String, String> filterMap = new LinkedHashMap<>();

        filterMap.put("/webjars/**", "anon");
        filterMap.put("/druid/**", "anon");
        filterMap.put("/app/**", "anon");
        filterMap.put("/sys/login", "anon");
        filterMap.put("/swagger/**", "anon");
        filterMap.put("/v2/api-docs", "anon");
        filterMap.put("/swagger-ui.html", "anon");
        filterMap.put("/swagger-resources/**", "anon");
        filterMap.put("/admin/captcha", "anon");
        filterMap.put("/logout", "logout");
        filterMap.put("/favicon.ico","anon");
//        filterMap.put("/hello/test", "anon");
//        filterMap.put("/admin/user", "anon");
        filterMap.put("/admin/captcha", "anon");
        filterMap.put("/**", "oauth2");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        return shiroFilterFactoryBean;
    }

    @Bean
    public SecurityManager securityManager(){
        //web项目默认的密码管理
        DefaultWebSecurityManager securityManager =  new DefaultWebSecurityManager();
        securityManager.setRealm(shiroRealm());
        return securityManager;
    }

    @Bean
    public ShiroRealm shiroRealm(){
        ShiroRealm shiroRealm = new ShiroRealm();
        return shiroRealm;
    }
}
